package animix.blue.server.user.service.impl;


import animix.blue.server.common.bean.pojo.Result;
import animix.blue.server.common.bean.model.Role;
import animix.blue.server.common.bean.tool.PagedList;
import animix.blue.server.common.bean.tool.Pagination;
import animix.blue.server.user.mapper.RoleMapper;
import animix.blue.server.user.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Result list(Map<String, Object> para) {
        // 分页插件
        Integer currentPage = para.get("currentPage") == null ? 1 : Integer.parseInt((String) para.get("currentPage"));
        Integer pageSize = para.get("pageSize") == null ? 10 : Integer.parseInt((String) para.get("pageSize"));
        PageHelper.startPage(currentPage, pageSize);
        // 查询
        List<Role> list = roleMapper.list(para);
        // 分页信息
        PageInfo pageInfo = new PageInfo(list);
        Pagination pagination = new Pagination();
        pagination.setCurrent(currentPage);
        pagination.setPageSize(pageSize);
        pagination.setTotal(pageInfo.getTotal());
        // 包装
        PagedList<Role> pagedList = new PagedList();
        pagedList.setList(list);
        pagedList.setPagination(pagination);
        // 返回
        return Result.ok(pagedList);
    }

    @Override
    public Result getById(Integer id) {
        if (id != null) {
            Role role = roleMapper.getById(id);
            if (role != null) {
                return Result.ok(role);
            } else {
                return Result.build(400, "id不存在！");
            }
        } else {
            return Result.build(400, "id无效！");
        }
    }

    @Override
    public Result add(Role role) {
        if (role == null) {
            return Result.build(400, "请求参数无效！");
        }
        // 非空验证
        if (StringUtils.isBlank(role.getName())) {
            return Result.build(400, "角色名称不能为空！");
        }
        // 重复验证
        Role roleCheck = roleMapper.getByName(role.getName());
        if (roleCheck != null) {
            return Result.build(400, "角色名称已存在！");
        }
        // 补全属性
        role.setCreated(new Date());
        role.setUpdated(new Date());
        // 添加
        roleMapper.add(role);
        return Result.ok(role);
    }

    @Override
    public Result updateById(Role role) {
        if (role == null) {
            return Result.build(400, "请求参数无效！");
        }
        // id验证
        Result result = getById(role.getId());
        if (result.getCode() != 200) {
            return result;
        }
        // 非空验证
        if (StringUtils.isBlank(role.getName())) {
            return Result.build(400, "角色名称不能为空！");
        }
        // 重复验证
        Role roleCheck = roleMapper.getByName(role.getName());
        if (roleCheck != null && !roleCheck.getId().equals(role.getId())) {
            return Result.build(400, "角色名称已存在！");
        }
        // 更新属性
        role.setUpdated(new Date());
        // 更新
        roleMapper.updateByIdSelective(role);
        return Result.ok();
    }
}
